package com.make.house.api.controller.wxcontroller.pay;

import com.make.house.api.common.ECConstant;
import com.make.house.api.controller.wxcontroller.login.WxLoginRespneseBean;
import com.make.house.api.entity.user.UserAuthEntity;
import com.make.house.api.model.ActionStatus;
import com.make.house.api.model.JSONResult;
import com.make.house.api.repository.user.UserAuthRepository;
import com.make.house.api.utils.WxOrderUtils;
import com.make.house.api.wxpay.WXPay;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yanglinlong on 18/8/27.
 */
@RestController
@RequestMapping(ECConstant.API_REQUEST_WX_URL+"/pay")
@Api("微信支付相关")
public class WXPayController {

    private static Logger logger = LoggerFactory.getLogger(WXPayController.class);

    @Autowired
    private UserAuthRepository userAuthRepository;

    private static final String WX_ORDER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    @ApiOperation(value="下单", notes="微信支付下单", response = WxLoginRespneseBean.class)
    @ApiImplicitParams({

            @ApiImplicitParam(name = "uid", value = "用户uid", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "平台(Android或iOS)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "平台(Android或iOS)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "平台(Android或iOS)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "平台(Android或iOS)", required = false, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    public JSONResult order(@ApiIgnore WXOrderRequestParas paras){

        UserAuthEntity userAuthEntity = userAuthRepository.findByUid(paras.getUid());
        if (userAuthEntity == null){
            return JSONResult.newBuilder().setStatus(ActionStatus.USER_NOT_EXIST.inValue())
                    .setDesc(ActionStatus.USER_NOT_EXIST.getDescription()).build();
        }
        MyWXPayConfig config = new MyWXPayConfig();
        try {
            // TODO: 18/8/29 参数不完整
            WXPay wxPay = new WXPay(config);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", "腾讯充值中心-QQ会员充值");
            data.put("out_trade_no", WxOrderUtils.generateOrderID());
            data.put("device_info", "");
            data.put("fee_type", "CNY");
            data.put("total_fee", "1");
            data.put("spbill_create_ip", "");
            data.put("notify_url", "http://www.example.com/wxpay/notify");
            data.put("trade_type", "JSAPI");  // 此处指定为扫码支付
            data.put("product_id", "12");
            String[] time = WxOrderUtils.generateOrderTime();
            data.put("time_start", time[0]);
            data.put("time_expire", time[1]);
            data.put("openid", userAuthEntity.getIdentifier());

            Map<String, String> resp = wxPay.unifiedOrder(data);
            logger.info("微信下单结果：" + resp);
        }catch (Exception e){
            return JSONResult.failureResult(e.getMessage());
        }
        return JSONResult.NO_RESULT;
    }

}
